package com.hqyj.demo.config;
import com.hqyj.demo.utils.AdminRealm;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//shiro配置信息
@Configuration
public class ShiroConfig {

    // shiroFilterFactoryBean   过滤所有请求的地方
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);

        return shiroFilterFactoryBean;
    }
    //     安全管理器  secrity Manager
    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager(Realm realm){
        DefaultWebSecurityManager webSecurityManager = new DefaultWebSecurityManager();
//       当前用户交给安全管理器识别
        webSecurityManager.setRealm(realm);
        return webSecurityManager;
    }
    //    当前用户  realm
    @Bean
    public Realm realm(){
//        自定义的ream
        AdminRealm adminRealm = new AdminRealm();
        //更改系统的加密规则
//      需要在用户中去设置
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
//       告诉shiro此刻使用的是md5加密算法
        hashedCredentialsMatcher.setHashAlgorithmName("md5");
        adminRealm.setCredentialsMatcher(hashedCredentialsMatcher);
        return adminRealm;
    }
}
